package test.testView2D;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

import view.view2D.Assets;

public class CanvasDiProva  extends Canvas implements Runnable, KeyListener{

	private static final long serialVersionUID = -6183750231313153186L;
	private BufferedImage img = null;
	private int x = 250, y = 0 , w = 200, h = 200;
	private Thread myThread;
	private boolean running = false;
	
	
	public CanvasDiProva() {
		super();
		myThread = new Thread(this);
//		this.g = this.getGraphics();
		this.img = Assets.getsLoader().getAsset(Assets.MOB_2D);
	}
	
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		draw(g);
	}
	
	public void draw(Graphics g){
		g.drawImage(img, x, y, w, h, this);
		g.dispose();
	}

	
	@Override
	public void run() {
		while(true){
			
			if(y > 600){
				y = 0;
			}
			y++;
			repaint();
//			System.out.println("mosso");
			try {
				myThread.sleep(10);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
	}
	
	
	@Override
	public void keyPressed(KeyEvent e) {
		if(!running){
			running = true;
			myThread.start();;
		}
	}
	
	
	
	
	@Override
	public void keyReleased(KeyEvent e) {
		System.out.println("released");
	}
	
	@Override
	public void keyTyped(KeyEvent e) {
	}
	
}
